<?php

/**
 * @package Nina
 * @author John Main <john@thedaddy.org>
 */
abstract class Nina_Model_Setting extends Nina_Model
{
	/**
	 * Check if this is a textarea field
	 * @return boolean
	 */
	public function getTextarea() {
		if($this->hasField('TEXTAREA'))
			return parent::getTextarea();
		else
			return false;
	}

	/**
	 * Check if this is a checkbox field
	 * @return boolean
	 */
	public function getCheckbox() {
		if($this->hasField('CHECKBOX'))
			return parent::getCheckbox();
		else
			return false;
	}

	/**
	 * Retrieve a setting
	 * @param string $name The setting name
	 * @return string|false
	 */
	public static function get($name) {
		$c = self::newCriteria();
		
		if(defined('DOMAIN')) {
			$c1 = Setting::newCriteria();
			$c1->add(Setting::DOMAIN, DOMAIN);
			$c1->addOr(Setting::DOMAIN, '');
			$c1->addOr(Setting::DOMAIN, null, Nina_Orm_Criteria::IS);
			$c->add($c1);
		}
		
		$c->add(Setting::PROPERTY, $name);
		if($setting = self::doSelectOne(new Setting(), $c)) {
			return $setting->getValue();
		}
		else
			return false;
	}
	/**
	 * Retrieve all settings
	 * @return array
	 */
	public static function getAll() {
		$c = self::newCriteria();
		
		if(defined('DOMAIN')) {
			$c1 = Setting::newCriteria();
			$c1->add(Setting::DOMAIN, DOMAIN);
			$c1->addOr(Setting::DOMAIN, '');
			$c1->addOr(Setting::DOMAIN, null, Nina_Orm_Criteria::IS);
			$c->add($c1);
		}
		
		return self::doSelect(new Setting(), $c);
	}
	
	/**
	 * Store a setting
	 * @param string $name The setting name
	 * @param string $value The setting value
	 * @param boolean [$domain_only=false] Set for this domain only
	 * @param boolean [$textarea=false] Set as textarea
	 * @param boolean [$checkbox=false] Set as checkbox
	 */
	public static function set($name, $value, $domain_only = false, $textarea = false, $checkbox = false) {
		$new_property = self::toUnderscore($name);
		
		if(self::get($new_property) === false) {

			$setting = new Setting();
			$setting->setProperty($new_property);
			$setting->setValue($value);
			
			if(defined('DOMAIN') && $domain_only)
				$setting->setDomain(DOMAIN);

			if($setting->hasField('TEXTAREA') && $textarea)
				$setting->setTextarea(true);

			if($setting->hasField('CHECKBOX') && $checkbox)
				$setting->setCheckbox(true);
	
			$setting->save();
		}
	}
	
	/**
	 * Store settings in bulk from request
	 */
	public static function setFromRequest() {
	
		$reg = Nina_Registry::getInstance();
		$request = $reg->get("runtime.request");
	
		if($request->getParameter('new_property')) {
			Setting::set($request->getSanitisedParameter('new_property'), $request->getSanitisedParameter('new_value'), $request->getParameter('domain_only'), $request->getParameter('textarea'), $request->getParameter('checkbox'));
		}

		$c = self::newCriteria();
		
		if(defined('DOMAIN')) {
			$c1 = Setting::newCriteria();
			$c1->add(Setting::DOMAIN, DOMAIN);
			$c1->addOr(Setting::DOMAIN, '');
			$c1->addOr(Setting::DOMAIN, null, Nina_Orm_Criteria::IS);
			$c->add($c1);
		}

		if ($settings = self::doSelect(new Setting(), $c)) {
			foreach ($settings as $setting) {

				$name = $setting->getProperty();
				if ($request->hasParameter($name)) {
					if($setting->hasField('CHECKBOX') && $setting->getCheckbox())
						$value = true;
					else
						$value = $request->getSanitisedParameter($name);
					
					$setting->setValue($value);
					$setting->save();
				}
				elseif($setting->hasField('CHECKBOX') && $setting->getCheckbox()) {
					$setting->setValue(false);
					$setting->save();
				}
			}
		}
	}
	
	/**
	 * Underscore and lowercase a string
	 * @param string $str The string
	 * @return string
	 */
	public static function toUnderscore($str) {
		$str = strtolower($str);
		$str = str_replace(' ', '_', $str);
		return $str;
	}
	
	/**
	 * Remove underscores and uppercase words in a string
	 * @param string $str The string
	 * @return string
	 */
	public static function toDescription($str) {
		$str = str_replace('_', ' ', $str);
		$str = ucwords($str);
		return $str;
	}
}
